package com.example.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.AfterSale;
import com.example.orderassembly.mapper.AfterSaleMapper;
import com.example.orderassembly.service.IAfterSaleService;
import com.woniuxy.form.GetAfterSaleByPlatform;
import com.woniuxy.form.InsertAfterSale;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 售后表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
@SuppressWarnings("all")
public class AfterSaleServiceImpl extends ServiceImpl<AfterSaleMapper, AfterSale> implements IAfterSaleService {
    @Resource
    AfterSaleMapper afterSaleMapper;

    @Override
    public Boolean updateAfterSaleById(Integer afterSaleId, Integer status) {
        AfterSale afterSale = afterSaleMapper.selectById(afterSaleId);

        if((afterSale.getAfterSaleStatus()==1)&&(status==2||status==3)){
            UpdateWrapper<AfterSale> afterSaleUpdateWrapper = new UpdateWrapper<>();
            afterSaleUpdateWrapper.set("after_sale_status",status);
            afterSaleUpdateWrapper.eq("after_sale_id",afterSaleId);
            int update = afterSaleMapper.update(null, afterSaleUpdateWrapper);
            return update ==1 ;
        }
        return false;
    }

    @Override
    @Cacheable(cacheNames = "cache:user211",key="#indexPage")
    public IPage getAfterSaleByUserId(Integer userId,Integer indexPage, Integer pageSize) {
        QueryWrapper<AfterSale> afterSaleQueryWrapper = new QueryWrapper<>();
        afterSaleQueryWrapper.eq("after_sale_user_id",userId);
        Page<AfterSale> afterSalePage = new Page<>();
        afterSalePage.setCurrent(indexPage);
        afterSalePage.setSize(pageSize);
        IPage<AfterSale> afterSaleIPage = afterSaleMapper.selectPage(afterSalePage, afterSaleQueryWrapper);
        return afterSaleIPage;
    }

    @Override
    public IPage getAfterSaleByPlatform(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        //设置分页
        Page<AfterSale> afterSalePage = new Page<>();
        afterSalePage.setCurrent(getAfterSaleByPlatform.getIndexPage());
        afterSalePage.setSize(getAfterSaleByPlatform.getPageSize());
        QueryWrapper<AfterSale> afterSaleQueryWrapper = new QueryWrapper<>();
        //根据情况查找
        if(getAfterSaleByPlatform.getStartDate()==null||getAfterSaleByPlatform.getEndDate()==null){
            if(getAfterSaleByPlatform.getStatus()==null){
            }else  {
                afterSaleQueryWrapper.eq("after_sale_status",getAfterSaleByPlatform.getStatus());
            }
        }else {
            afterSaleQueryWrapper.between("raw_add_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
            if(getAfterSaleByPlatform.getStatus()==null){
            }else {
                afterSaleQueryWrapper.eq("after_sale_status",getAfterSaleByPlatform.getStatus());
            }
        }
        IPage<AfterSale> afterSaleIPage = afterSaleMapper.selectPage(afterSalePage, afterSaleQueryWrapper);
        return afterSaleIPage;
    }

    @Override
    public Boolean insertAfterSale(InsertAfterSale insertAfterSale) {
        AfterSale afterSale = new AfterSale();
        //JWT 获取
        Integer userId = 1;
        BeanUtils.copyProperties(insertAfterSale,afterSale);
        afterSale.setAfterSaleUserId(userId);
        System.out.println(afterSale);
        int insert = afterSaleMapper.insert(afterSale);
        return insert==1;
    }

    @Override
    public IPage getAfterSaleByStore(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        //todo 根据店铺JWT 获取店铺id
        Integer storeId =1;
        QueryWrapper<AfterSale> afterSaleQueryWrapper = new QueryWrapper<>();
        afterSaleQueryWrapper.eq("after_sale_store_id",storeId);
        Page<AfterSale> afterSalePage = new Page<>();
        afterSalePage.setCurrent(getAfterSaleByPlatform.getIndexPage());
        afterSalePage.setSize(getAfterSaleByPlatform.getPageSize());
        if(getAfterSaleByPlatform.getStartDate()==null||getAfterSaleByPlatform.getEndDate()==null){
            if(getAfterSaleByPlatform.getStatus()==null){
            }else  {
                afterSaleQueryWrapper.eq("after_sale_status",getAfterSaleByPlatform.getStatus());
            }
        }else {
            afterSaleQueryWrapper.between("raw_add_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
            if(getAfterSaleByPlatform.getStatus()==null){
            }else {
                afterSaleQueryWrapper.eq("after_sale_status",getAfterSaleByPlatform.getStatus());
            }
        }
        IPage<AfterSale> afterSaleIPage = afterSaleMapper.selectPage(afterSalePage, afterSaleQueryWrapper);
        return afterSaleIPage;
    }

    @Override
    public AfterSale getAfterSaleById(Integer afterSaleId) {
        AfterSale afterSale = afterSaleMapper.selectById(afterSaleId);
        //TODO 查询店铺，用户，订单，类型1.退货  2.换货   状态 1.未完成   2.已完成   3.不能售后

        return afterSale;
    }
}
